/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file SCDatabase.h
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#ifndef SCDATABASE_H_
#define SCDATABASE_H_

#include <FIo.h>
#include <FBase.h>

#include "models/SCAlarm.h"
#include "models/SCSession.h"

using namespace Osp::Io;
using namespace Osp::Base;

class SCDatabase : public Osp::Base::Runtime::Thread
{

// Constructor
private:
	SCDatabase();
	virtual ~SCDatabase();

// Constants
public:
	static const int ACTION_DELETE		= 100;
	static const int ACTION_MODIFY		= 101;
	static const int ACTION_ADD			= 102;

// Attributes
private:
	static Database *pDatabase;
	static DbStatement* pAlarmInsertStmt;
	static DbStatement* pAlarmUpdateStmt;
	static DbStatement* pAlarmRepeatInsertStmt;
	static DbStatement* pAlarmRepeatUpdateStmt;
	static DbStatement* pSessionInsertStmt;
	static DbStatement* pSessionUpdateStmt;
	static DbStatement* pSampleInsertStmt;
	static DbStatement* pSampleUpdateStmt;
	static ArrayList* pAlarmsList;
	static ArrayList* pSessionsList;
	static bool reloadSessions;
	static bool reloadSessionsDelete;
	static bool reloadAlarms;
	static bool reloadAlarmsDelete;
	static ArrayList* pDataModificationList;

// Methods
public:
	static result Construct();
	static result Destruct();
	static void CreateStatements();

	//Cache
	static ArrayList* GetAllAlarms();
	static ArrayList* GetAllSessions();
	static ArrayList* LoadAllAlarms();
	static ArrayList* LoadAllSessions();
	static bool GetReloadSessions();
	static void SetReloadSessions(bool value);
	static bool GetReloadSessionsDelete();
	static void SetReloadSessionsDelete(bool value);
	static bool GetReloadAlarms();
	static void SetReloadAlarms(bool value);
	static bool GetReloadAlarmsDelete();
	static void SetReloadAlarmsDelete(bool value);
	static ArrayList* GetDataModificationList();
	static void AddDataModification(int operationType, RequestId formId, int itemId);

	//Alarms
	static bool SaveAlarm(SCAlarm *pAlarm);
	static bool LoadAlarm(SCAlarm *pAlarm);
	static SCAlarm* GetFirstActiveAlarm();
	static void DeleteAlarm(int id);
	static SCAlarm* GetAlarm(int id);
	//AlarmRepeat
	static bool SaveAlarmRepeat(SCAlarmRepeat *pAlarmRepeat);
	static bool LoadAlarmRepeat(SCAlarmRepeat *pAlarmRepeat);
	static void DeleteAlarmRepeat(int id);
	//Session
	static bool SaveSession(SCSession *pSession);
	static bool LoadSession(SCSession *pSession);
	static void DeleteSession(int id);
	static SCSession* GetLastSession();
	static ArrayList* GetAllSamplesOfSession(int sessionId);
	static SCSession* GetSession(int id);
	//Sample
	static bool SaveSample(SCSample *pSample);
	static bool LoadSample(SCSample *pSample);
	static void DeleteSample(int id);
	//Settings
	static bool LoadSettings();
	static bool SaveSettings();

private:
	static result CreateSCDatabase(Database *database);
	static int GetLastInsertId(String tableName);
};

#endif /* SCDATABASE_H_ */
